 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pro_sewa_gedung extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('pro_sewa_gedung_model', 'Tempat');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Sewa Gedung";

		viewByTemplate('pro_sewa_gedung/index', $data, 'grid');
	}
	

	function create()
	{
		// Display Form Create
		$data['title'] = "Input Tempat Baru";
		$data['data'] = NULL;
		//$data['arr_reference']	= $this->Combo->SampleReference();
		$data['mode'] = "create";
		$data['lokasi_tempat'] = $this->Tempat->getApp();
		$query = $this->db->query("SELECT id_tempat FROM (SELECT * FROM pro_sewa_gedung ORDER BY id_tempat DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['id_tempat'];
		else
			$MaxID = "TK0000000"; 

		$NextID = (int) substr($MaxID,2,7);

		$NextID++;
		
		$data['id_tempat'] = "TK".sprintf("%07s",$NextID);

		$query1 = $this->db->query("SELECT id_sewa FROM (SELECT * FROM pro_sewa_gedung ORDER BY id_sewa DESC) WHERE rownum <=1");
				 				if ($query1->row_array()) {
				 					$getquery1 = $query1->row_array();
				 					$rowcount1 = $getquery1['id_sewa'];
				 					$autonum1 = $rowcount1 + 1;
				 				} else {
								$autonum1 = 1;
							}
		$data['id_sewa_auto'] = $autonum1;
		viewByTemplate('pro_sewa_gedung/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Data Tempat";
		$data['data'] = $this->Tempat->GetSingleView(get('id_sewa'));
		$data['id_sewa_edit']=get('id_sewa');
		$data['id_lokasi_edit']=get('id_lokasi');
		$data['lokasi_tempat'] = $this->Tempat->getApp();
		// Define Combo Data
		$data['mode'] = "edit";
		viewByTemplate('pro_sewa_gedung/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Data Tempat";
		$data['data'] = $this->Tempat->GetSingleView(get('id_sewa'));
		
		$data['mode'] = "view";
		viewByTemplate('pro_sewa_gedung/form', $data, 'form');
	}

	function getIdAuto($type=null) {
		$id = $this->input->post('option');
		//var_dump($id);
		//$this->secure_session();

		$ls_sql	= "	SELECT 	a.id_lokasi as key_lokasi,
							b.lokasi_tempat as id_lokasi,
							a.no_toko as no_toko
					FROM pro_tenant a 
					JOIN pro_lokasi_tempat b ON a.id_lokasi = b.id_lokasi
					WHERE a.status_tenant = 'Tenant' AND a.lokasi_tempat = '".$id."'
					ORDER BY REGEXP_REPLACE(a.no_toko, '[0-9]', ''), TO_NUMBER(REGEXP_SUBSTR(a.no_toko, '[0-9]+')) ";

		$result	= $this->db->query($ls_sql);
		$data_toko = $result->result_array(); 

		echo json_encode($data_toko);

		//echo $this->KontrolPemakaian->getNotoko($id);	
	}

	function getIdLokasi($type=null) {
		$id = $this->input->post('option');
		//var_dump($id);
		//$this->secure_session();

		$ls_sql	= "	SELECT 	a.id_lokasi,a.lokasi_tempat
					FROM pro_lokasi_tempat a 
					WHERE a.lokasi_tempat = '$id'";

		$result	= $this->db->query($ls_sql);
		$id_lokasi = $result->row_array(); 

		//var_dump($id_lokasi);

		echo json_encode($id_lokasi);

		//echo $this->KontrolPemakaian->getNotoko($id);	
	}

	function getdata()
	{
		$this->secure_session();
		echo $this->Tempat->GetJSON();
	}

	function getdataLOV()
	{
		$this->secure_session();
		echo $this->Tempat->GetJSONLOV();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		if (post("id_tempat") == '') {
			$error->Add("id_tempat", "ID Tempat", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM pro_sewa_gedung WHERE id_tempat= '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("no"))))."'")) > 0) {
			$error->Add("id_tempat", "ID Tempat", $tpl_duplicate);
		}/*else if (post("no_toko") == '') {
			$error->Add("no_toko", "No Toko", $tpl_empty);
		}*/else if (post("lokasi_tempat") == '') {
			$error->Add("lokasi_tempat", "Lokasi Tempat", $tpl_empty);
		}/*else if (post("luas_tempat") == '') {
			$error->Add("luas_tempat", "Luas Tempat", $tpl_empty);
		}else if (post("harga_sewa") == '') {
			$error->Add("harga_sewa", "Harga Sewa", $tpl_empty);
		}*/else if (post("status_sewa") == '') {
			$error->Add("status_sewa", "Status Sewa", $tpl_empty);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data = array(	'id_tempat' => $this->input->post('id_tempat'),
								'no_toko' => $this->input->post('no_toko'),
								'lokasi_tempat' => $this->input->post('lokasi_tempat'),
								'luas_tempat' => $this->input->post('luas_tempat'),
								'harga_sewa' => $this->input->post('harga_sewa'),
								'status_sewa' => $this->input->post('status_sewa'),
								'id_lokasi' => $this->input->post('id_lokasi'),
								'id_sewa' => $this->input->post('id_sewa'),
							);

				if (post("mode") == "create")
				{
					

					$this->Tempat->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->Tempat->Update($data, array('id_tempat'	=> $data['id_tempat']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$pesan = "Data Terhapus";
		$id_sewa	= get('id_sewa');
		
		if ($id_sewa != "")
		{
			// Delete Single
			$arr_rule	= array('id_sewa' => $id_sewa);
			$ls_result = $this->Tempat->DeleteSingle($arr_rule);

		}
		else
		{
		}
		
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_sewa' => $row['id_sewa']);
				$ls_result .= $this->Tempat->DeleteSingle($arr_rule);
			}

		echo $ls_result;
		echo $pesan;
	}
}
?>